/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.base;

import lombok.Getter;
import lombok.Setter;

import org.apache.log4j.Level;

import com.google.code.cubeirc.Main;
import com.google.code.cubeirc.basequeue.BaseQueue;
import com.google.code.cubeirc.common.Utils;
import com.google.code.cubeirc.debug.DebuggerQueue;
import com.google.code.cubeirc.queue.MessageQueue;
import com.google.code.cubeirc.queue.MessageQueueEvent;
import com.google.code.cubeirc.queue.MessageQueueListener;

public class Base implements MessageQueueListener, IBase {

	@Getter @Setter
	private String name;

	public Base(String name) {
		setName(name);
		addDebug(Level.DEBUG,"Base class created %s",getName());
		MessageQueue.addListener(this);
		BaseQueue.addComponent(this);
		this.Init();
	}

	/**
	 * Execute closing of class
	 * @return
	 */
	public boolean Close()
	{
		addDebug(Level.DEBUG, "%s: Executing Close() function",getName());
		MessageQueue.removeListener(this);
		return true;
	}

	/**
	 * Execute after creation of class
	 */
	public void Init()
	{
		addDebug(Level.DEBUG, "%s: Executing Init() function",getName());
	}

	@Override
	public void actionPerformed(MessageQueueEvent e) 
	{		

	}

	/**
	 * Exec async Thread
	 * @param runnable
	 */
	protected void asyncExec(Runnable runnable)
	{
		if (!Main.getShell().isDisposed())
			Utils.asyncExec(Main.getShell().getDisplay(), runnable);
	}

	/**
	 * Add message to debugger with sender
	 * @param level
	 * @param text
	 * @param args
	 */
	protected void addDebug(Level level, String text, Object ... args)
	{
		DebuggerQueue.addDebug(this, level, text, args);

	}
}
